home *** CD-ROM | disk | FTP | other *** search
- Path: news.magg.net!news
- From: n4mwd@magg.net (Dennis Hawkins)
- Newsgroups: comp.lang.c
- Subject: Re: I need help on putting 2 strings together into 1(concatenate)
- Date: Wed, 21 Feb 1996 01:26:22 GMT
- Organization: M.A.G. Information Services (MAGG.NET)
- Message-ID: <4gdouc$p36@dopey.magg.net>
- References: <4gcr1b$ft3@cloner4.netcom.com>
- NNTP-Posting-Host: wpb-126.magg.net
- X-Newsreader: Forte Free Agent 1.0.82
-
- spdcool@ix.netcom.com(SPD) wrote:
-
- > I figured out my earlier problem. Thanks to all that helped. Now I
- >have another problem. First take a look at a short version of my code:
-
- >#include <stdio.h>
- >#include <stdlib.h>
- >#include <string.h>
- >#include <time.h>
- >struct data {
- > char *full_name;
- ^^^^^^^^^^^ You are NOT defining storage, just a
- pointer.
- > int age;
- > float salary;
- > };
-
- >struct name {
- > char *first1, *last1;
- > };
-
-
- >int main(void)
- >{
- > struct name fname[3], lname[3];
- ^^^^^ since name is defined with both a pointer to first and last
- name, why do you need another fname and lname??? Why not just
- struct name names[3];
- > struct data secret[5];
- > int i, ran;
-
- > fname[0].first1 = "Joe";
- ^^^^^^ This is not copying a string,
- only a pointer. The space for "Joe" is auto-initialized with 4 bytes
- of storage.
- > fname[1].first1 = "Michael"; <- 8 bytes
- > fname[2].first1 = "Bruce"; <- 6 bytes
-
- > lname[0].last1 = "Jordan"; <- 7 bytes
- > lname[1].last1 = "Willis"; <- 7 bytes
- > lname[2].last1 = "Jackson"; <- 8 bytes
-
- > randomize()
- > for (i = 0; i < 5; ++i) {
- > e = random(3);
- > secret[i].full_name = fname[e].first1;/* the trouble some part begin
- ^^^^^ Now points to the auto storage of at most 8 bytes
- (Michael).
- > e = random(3);
- > strcat(secret[i].full_name," ");
- ^^^^^^ overwrite the null terminator of the auto storage
- If full_name pointed to "Michael", now it points to "Michael Bruce"
- > strcat(secret[i].full_name,lname[e].last1); /*trouble ends here*/
- ^^^^^
- Assuming that the first name selected was Michael and the second was
- Jordan, you have now overwritten memory by 8 bytes " Jordan". At
- best, this will result in some bizarre string outputs. But most
- likely, your program will crash.
- > "
- > "
- > " /* the rest works */
- > " }
- > for (i = 0; i < 5; ++i) {
- > printf("%s%s",secret[i].full_name, " ");
- ^^^^ Whats the " " for?? What wrong with
- printf("%s ",secret[i].full_name);
- > printf("%d%s%5.2f\n", secret[i].age," ",secret[i].salary);
- ^^^^ More spaces, try:
- printf("%d %5.2f\n", secret[i].age,secret[i].salary);
- > }
- > return 0;
- >}
-
-
- Why not just try:
-
-
- for (i = 0; i < 5; ++i)
- {
- printf("%s %s",names[random(3)].first1, names[random(3)].last1);
- }
-
- Your code was quite buggy. You should consider using Turbo Debugger
- on this type of an app. TD would have found the bug for you in much
- less time than it has taken me to write this note. Also, you might
- consider using parallel arrays for the name storage. It is much more
- efficient than using structures. That way, all you would have to do
- would be auto-initialize the arrays and then:
- printf("%s %s", first[random(3)], last[random(3)]);
-
- Hope this helps.
-
- Dennis Hawkins
- n4mwd@amsat.org
-
-
-